
*	************************************************************************
* 	0. Directory
*	************************************************************************

*** Change directory: 
* cd ""
  
*	************************************************************************
* 	1. Table 1
*	************************************************************************

clear
use hh_data.dta, clear
set matsize 10000

local miglength migration_5y_liviewithinhh migration_5y_less3month migration_5y_3months migration_5y_moveoutperm ///
	migration_5y_winter migration_5y_summer migration_5y_rainy migration_5y_rab ///
  q301b_reason_pay q301b_reason_opportunity q301b_reason_landless q301b_reason_debt  ///
  q301g_occ_agri q301g_occ_industry q301g_occ_daily q301c_totown q301f_longestdist
  
eststo clear
eststo all,title("All migrating HH"): quietly estpost summarize ///
    `miglength'
eststo new,title("Mig start after 2013"): quietly estpost summarize ///
    `miglength' if migration_indic == 1
eststo old,title("Mig start before 2013"): quietly estpost summarize ///
    `miglength' if migration_indic == 2
eststo diff2,title("Aft-Bef 2013"): quietly estpost ttest ///
    `miglength', by(migration_indic2) unequal

esttab all new old diff2 using "table1.tex" , ///
cells("mean(pattern(1 1 1 0) fmt(2) label(\% Migrating HH)) b(star pattern(0 0 0 1) fmt(2) label(T-test))") ///
star(* 0.10 ** 0.05 *** 0.01) label mtitle legend replace


*	************************************************************************
* 	2. Table 2
*	************************************************************************


local povertyassess q206_casteSCST q207_levelschooling_prim q207_levelschooling_sec q204_childrennum2 q209_BPL_card  ///
q605_5ago_montlyexpk  q606_5ago_expeduck  q608_5ago_bankamountk2  q609_5ago_savingamountk ///
q405_mobile_2013 q402_pucca_2013 q406_tv_2013 q409_motorbike_2013 q411_toilet_2013 q412_watersource_2013 ///
q413_cattle_5ago2 q414a_5ago_landamount_acre2 q414b_5ago_irrigatedland_acre2 ///

* Table of demographics
eststo clear
eststo nomig,title("No mig"): quietly estpost summarize ///
   `povertyassess' if migration_indic == 0
eststo new,title("Mig starts after 2013"): quietly estpost summarize ///
    `povertyassess' if migration_indic == 1
eststo diff1,title("(1) - (2)"): quietly estpost ttest ///
    `povertyassess', by(migration_indic1) unequal
eststo old,title("Mig starts bef 2013"): quietly estpost summarize ///
    `povertyassess' if migration_indic == 2
eststo diff2,title("(1) - (4)"): estpost ttest ///
    `povertyassess', by(migration_indic2) unequal

esttab new nomig diff1 old diff2 using "table2.tex" , ///
cells("mean(pattern(1 1 0 1 0 ) fmt(2) label(Average)) b(star pattern(0 0 1 0 1 ) fmt(2) label(T-test))") ///
star(* 0.10 ** 0.05 *** 0.01) label mtitle legend replace


*	************************************************************************
* 	3. Table 3
*	************************************************************************

global depvar q705c_nosalarybetween q709c_landlossbetween q710c_sickdeathaccidentbetween  q206_casteSCST q207_levelschooling_sec 

eststo clear
eststo: reg q301j_remittances_alllog migration_5y_new $depvar i.q108_village_code, cluster(q108_village_code)
eststo: reg q605_montlyexp_logdiff migration_5y_new $depvar i.q108_village_code, cluster(q108_village_code)
eststo: reg q606_expeduc_logdiff  migration_5y_new $depvar q204_childrennum2 i.q108_village_code, cluster(q108_village_code)
eststo: reg q608_bankamount_logdiff migration_5y_new $depvar i.q108_village_code, cluster(q108_village_code)
eststo: reg q609_savingamount_logdiff migration_5y_new $depvar  i.q108_village_code, cluster(q108_village_code)
esttab using "table3.tex", booktabs replace label se(2) b(2) star(* 0.10 ** 0.05 *** 0.01) substitute(m$ m\\$ _ \_) compress eqlabels(none) nogaps sfmt(%9.2f %9.0f) order(migration_5y_new q204_childrennum2) indicate("Village FE=*q108_village_code*") 

*	************************************************************************
* 	4. Table 4
*	************************************************************************

eststo clear
eststo: probit q405_mobile_5y2 migration_5y_new $depvar  i.q108_village_code, cluster(q108_village_code)
eststo: probit q402_pucca_5y2 migration_5y_new $depvar i.q108_village_code, cluster(q108_village_code)
eststo: probit q406_tv_5y2 migration_5y_new $depvar  i.q108_village_code, cluster(q108_village_code)
eststo: reg q414a_landamount_acre_5y2 migration_5y_new $depvar  i.q108_village_code, cluster(q108_village_code) 
eststo: reg q414b_iriigatedland_acre_5y2 migration_5y_new $depvar  i.q108_village_code, cluster(q108_village_code) 
esttab using "table4.tex", booktabs replace label se(2) b(2) star(* 0.10 ** 0.05 *** 0.01) substitute(m$ m\\$ _ \_) compress eqlabels(none) nogaps sfmt(%9.2f %9.0f) indicate("Village FE=*q108_village_code*") 


*	************************************************************************
* 	5. Table 5
*	************************************************************************

eststo clear
eststo: reg q413_cattlenew_5y2 migration_5y_new $depvar i.q108_village_code, cluster(q108_village_code)
eststo: probit q507_lpg_5y2 migration_5y_new $depvar  i.q108_village_code, cluster(q108_village_code)
eststo: ologit q512_firewoodcollectioncompare migration_5y_new $depvar i.q108_village_code, cluster(q108_village_code)
eststo: ologit q513_grazecattlecompare migration_5y_new $depvar i.q108_village_code, cluster(q108_village_code)

esttab using "table5.tex", booktabs replace label se(2) b(2) star(* 0.10 ** 0.05 *** 0.01) substitute(m$ m\\$ _ \_) compress eqlabels(none) nogaps drop(cut*) sfmt(%9.2f %9.0f) indicate("Village FE=*q108_village_code*") 


*	************************************************************************
* 	6. Table 6
*	************************************************************************

clear
use village_data.dta, clear
set matsize 10000

* Variables and varlists
gen migration_5y2=migration_5y*migration_5y
label var migration_5y2 "All Migration 5y,\%, squared"

global depvarvill  q206_casteSCST q207_levelschooling_sec Road_Near City_Near Prot_Near 

* Program to append models
cap program drop appendmodels
program appendmodels, eclass
// using first equation of model
 syntax namelist
 tempname b V tmp
 foreach name of local namelist {
     qui est restore `name'
     mat `tmp' = e(b)
     local eq1: coleq `tmp'
     gettoken eq1 : eq1
     mat `tmp' = `tmp'[1,"`eq1':"]
     local cons = colnumb(`tmp',"_cons")
     if `cons'<. & `cons'>1 {
         mat `tmp' = `tmp'[1,1..`cons'-1]
     }
     mat `b' = nullmat(`b') , `tmp'
     mat `tmp' = e(V)
     mat `tmp' = `tmp'["`eq1':","`eq1':"]
     if `cons'<. & `cons'>1 {
         mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
     }
     capt confirm matrix `V'
     if _rc {
         mat `V' = `tmp'
     }
     else {
         mat `V' = ///
         ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
         ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
    }
}
local names: colfullnames `b'
mat coln `V' = `names'
mat rown `V' = `names'
eret post `b' `V'
eret local cmd "whatever"
end

* Regressions and implied parameter
eststo clear
foreach i of numlist 1 2 3 5 8 10 {
gen tcpop=perctc_`i'/totalpopulationofvillage
eststo b1`i':reg avgbgi_`i' migration_5y migration_5y2 $depvarvill tcpop i.q109_district_code, cluster(q109_district_code)
local treb_r2 = e(r2)
local treb_N = e(N)
cap gen avgbgi_`i'2 = _b[_cons] +_b[migration_5y]*migration_5y + _b[migration_5y2]*migration_5y2
cap nlcom (Deg_Max_Mig: -_b[migration_5y]/(2*_b[migration_5y2])), post
estimates store b2`i'
appendmodels b1`i' b2`i'	
estimates store a`i'						
estadd local FE "District FE"
estadd scalar treb_r2 = `treb_r2'
estadd scalar treb_N = `treb_N'
estimates store a`i'
drop tcpop
}


* Table
gen tcpop=.
label var tcpop "\% Tree Cover / 2011 Population (k)"

esttab a1 a2 a3 a5 a8 a10 using "table6.tex" , ///
se(2) b(2) ///
order(migration_5y migration_5y2 $depvarvill tcpop temp1 temp Deg_Max_Mig) ///
stats(FE treb_r2 treb_N, fmt(%6.3f %6.2f %6.0f) ///
labels("Fixed Effect" "R-squared" "Observations")) ///
varlabels(Deg_Max_Mig "for which degradation is maximum" ///
temp "Implied migration percentage" temp1 "\hline") ///
drop(_cons *q109_district_code* , relax) ///
star(* 0.10 ** 0.05 *** 0.01) label mtitles("1km" "2km" "3km" "5km" "8km" "10km") replace



*	************************************************************************
* 	7. Figure 4
*	************************************************************************

fracpoly regress avgbgi_8 migration_5y
fracplot migration_5y, msize(tiny) jitter(5) mcolor(black) lineopt(lcolor(blue)) ///
title("") ///
ytitle("Bare Ground Index (8km)") xtitle("Proportion of households with migrants in the village") plotregion(fcolor(white))  graphregion(fcolor(white)) 
graph export "figure4.png", replace	






